﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib_general;

namespace HAM
{
    public partial class frmRight_DN : Form
    {
        private DataSet ads;
        Exec_Database lib = new Exec_Database();
        //private System.Windows.Forms.TreeView treeView1;
        public frmRight_DN(TreeNode node)
        {
            InitializeComponent();

            treeView1.Nodes.Add(node);
        }

        private void frmRight_DN_Load(object sender, EventArgs e)
        {
            if (Screen.PrimaryScreen.WorkingArea.Width > 800) this.WindowState = FormWindowState.Normal;
            try
            {
                ads = lib.Get_data("select * from menuitem order by id");
                int i = int.Parse(ads.Tables[0].Rows[0][0].ToString());
            }
            catch
            {
                ads = null;
            }
            f_Set_Right();
        }
        private void f_Set_Right()
        {
            foreach (TreeNode anode in treeView1.Nodes)
            {
                if (anode.Nodes.Count > 0) f_Set_Right(anode);
                else
                {
                    if (ads.Tables[0].Select("id=" + anode.Tag.ToString()).Length > 0)
                    {
                        anode.Checked = true;
                    }
                }
            }
        }
        private void f_Set_Right(TreeNode v_node)
        {

            try
            {
                foreach (TreeNode anode in v_node.Nodes)
                {
                    if (anode.Nodes.Count > 0)
                    {
                        f_Set_Right(anode);
                    }
                    else
                    {
                        if (ads.Tables[0].Select("id=" + anode.Tag.ToString()).Length > 0)
                        {
                            anode.Checked = true;
                        }
                    }
                }
            }
            catch
            {
            }

        }
        private void f_Add_Idmenu_Cha(TreeNode v_node)
        {
            DataRow r;
            if (v_node.Parent != null)
            {
                if (ads.Tables[0].Select("id='" + v_node.Parent.Tag.ToString().PadLeft(3, '0') + "'").Length <= 0)
                {
                    r = ads.Tables[0].NewRow();
                    r["id"] = v_node.Parent.Tag.ToString().PadLeft(3, '0');
                    r["id_menu"] = v_node.Parent.Tag.ToString().PadLeft(3, '0');
                    r["ten"] = v_node.Parent.Text;
                    r["stt"] = v_node.Parent.Index;
                    if (v_node.Parent.Parent != null)
                    {
                        if (v_node.Parent.Parent.Tag.ToString() == "menuChucnang")
                        {
                            r["id_goc"] = "-1";
                        }
                        else r["id_goc"] = v_node.Parent.Parent.Tag.ToString().PadLeft(3, '0');
                    }
                    ads.Tables[0].Rows.Add(r);
                }
                if (v_node.Parent != null)
                {
                    //f_Add_Idmenu_Cha(v_node.Parent);
                }
            }
        }
       
        private bool bCheck_Menu_Con(TreeNode v_node)
        {
            foreach (TreeNode anode in v_node.Nodes)
            {
                if (anode.Checked)
                {
                    return true;
                }
            }
            return false;
        }

        private void f_Set_All(bool bAll)
        {
            foreach (TreeNode anode in treeView1.Nodes)
            {
                if (anode.Nodes.Count > 0)
                {
                    f_Chon_Quyen(anode, bAll);
                }
            }
        }
        private void f_Chon_Quyen(TreeNode v_node, bool v_bool)
        {
            try
            {
                foreach (TreeNode anode in v_node.Nodes)
                {
                    anode.Checked = v_bool;
                    if (anode.Nodes.Count > 0)
                    {
                        f_Chon_Quyen(anode, v_bool);
                    }
                }
            }
            catch
            {
            }
        }

        private void butAll_Click(object sender, EventArgs e)
        {
            butAll.Text = butAll.Text == "Chọn cả" ? "Bỏ cả" : "Chọn cả";
            if (butAll.Text == "Chọn cả") f_Set_All(false);
            else f_Set_All(true);
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            DataRow r;
            if (ads.Tables[0].Select("id='" + e.Node.Tag.ToString().PadLeft(3, '0') + "'").Length <= 0)
            {
                if (e.Node.Checked)
                {
                    r = ads.Tables[0].NewRow();
                    r["id"] = e.Node.Tag.ToString().PadLeft(3, '0');
                    r["id_menu"] = e.Node.Tag.ToString().PadLeft(3, '0');
                    r["ten"] = e.Node.Text;
                    r["stt"] = e.Node.Index;
                    if (e.Node.Parent != null)
                    {
                        if (e.Node.Parent.Tag.ToString() == "menuChucnang")
                        {
                            r["id_goc"] = "-1";
                        }
                        else
                        {
                            r["id_goc"] = e.Node.Parent.Tag.ToString().PadLeft(3, '0');
                        }
                    }
                    ads.Tables[0].Rows.Add(r);
                }
            }
            else
            {
                //if (!e.Node.Checked)
                //{
                //    r = kt.getrowbyid(ads.Tables[0], "id='" + e.Node.Tag.ToString().PadLeft(3, '0') + "'");
                //    if (r != null)
                //    {
                //        ads.Tables[0].Rows.Remove(r);
                //    }
                //}

            }

            try
            {
                if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
                {
                    f_Chon_Quyen(e.Node, e.Node.Checked);
                    if (e.Node.Checked)
                    {
                        f_Add_Idmenu_Cha(e.Node);
                    }
                    else
                    {
                        //f_Remove_Idmenu_Cha(e.Node);

                    }
                }
            }
            catch
            {
            }

        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            lib.Exec_data("delete from menuitem");
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void butLuu_Click(object sender, System.EventArgs e)
        {
            //try
            //{
            //    lib.Exec_data("delete from menuitem");
            //    foreach (DataRow r in ads.Tables[0].Rows)
            //    {
            //        upd_a_menuitem(r["id"].ToString(), r["id_menu"].ToString(), r["id_goc"].ToString(), decimal.Parse(r["stt"].ToString()), r["ten"].ToString());
            //    }
            //    foreach (DataRow r1 in kt.get_data("select * from a_dlogin").Tables[0].Rows)
            //    {
            //        string s = "";
            //        foreach (DataRow r2 in kt.get_data("select * from a_menuitem").Tables[0].Rows)
            //        {
            //            if (r1["right_"].ToString().IndexOf(r2["id_menu"].ToString() + "+") > -1)
            //            {
            //                s += r2["id_menu"].ToString() + "+";
            //            }
            //        }
            //        kt.execute_data("update a_dlogin set right_='" + s + "' where id=" + r1["id"].ToString() + "");
            //    }
            //    MessageBox.Show("Lưu thành công", kt.Msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //}
        }
	
    }
}
